import 'package:finance_app/src/entities/payment_entity.dart';
import 'package:finance_app/src/entities/account_entity.dart';
import 'package:finance_app/src/entities/ocr_attachment_entity.dart';
import 'package:finance_app/src/entities/related_payment_entity.dart';

paymentEntityFromJson(PaymentEntity data, Map<String, dynamic> json) {
	if (json['data_type'] != null) {
		data.dataType = json['data_type'].toString();
	}
	if (json['id'] != null) {
		data.id = json['id'] is String
				? int.tryParse(json['id'])
				: json['id'].toInt();
	}
	if (json['flow_type'] != null) {
		data.flowType = json['flow_type'].toString();
	}
	if (json['type'] != null) {
		data.type = json['type'].toString();
	}
	if (json['contact_id'] != null) {
		data.contactId = json['contact_id'] is String
				? int.tryParse(json['contact_id'])
				: json['contact_id'].toInt();
	}
	if (json['account_id'] != null) {
		data.accountId = json['account_id'] is String
				? int.tryParse(json['account_id'])
				: json['account_id'].toInt();
	}
	if (json['payment_type_id'] != null) {
		data.paymentTypeId = json['payment_type_id'] is String
				? int.tryParse(json['payment_type_id'])
				: json['payment_type_id'].toInt();
	}
	if (json['issued_at'] != null) {
		data.issuedAt = json['issued_at'].toString();
	}
	if (json['numbers'] != null) {
		data.numbers = (json['numbers'] as List).map((v) => v).toList().cast<dynamic>();
	}
	if (json['number_ids'] != null) {
		data.numberIds = (json['number_ids'] as List).map((v) => v).toList().cast<dynamic>();
	}
	if (json['currency_code'] != null) {
		data.currencyCode = json['currency_code'].toString();
	}
	if (json['total'] != null) {
		data.total = json['total'] is String
				? num.tryParse(json['total'])
				: json['total'];
	}
	if (json['cheque_number'] != null) {
		data.chequeNumber = json['cheque_number'].toString();
	}
	if (json['created_at'] != null) {
		data.createdAt = json['created_at'].toString();
	}
	if (json['updated_at'] != null) {
		data.updatedAt = json['updated_at'].toString();
	}
	if (json['name'] != null) {
		data.name = json['name'].toString();
	}
	if (json['account'] != null) {
		data.account = AccountEntity().fromJson(json['account']);
	}
	if (json['to_account'] != null) {
		data.toAccount = AccountEntity().fromJson(json['to_account']);
	}
	if (json['to_account_id'] != null) {
		data.toAccountId = json['to_account_id'] is String
				? int.tryParse(json['to_account_id'])
				: json['to_account_id'].toInt();
	}
	if (json['related_payment_id'] != null) {
		data.relatedPaymentId = json['related_payment_id'] is String
				? int.tryParse(json['related_payment_id'])
				: json['related_payment_id'].toInt();
	}
	if (json['related_payment'] != null) {
		data.relatedPayment = RelatedPaymentEntity().fromJson(json['related_payment']);
	}
	if (json['attachments'] != null) {
		data.attachments = (json['attachments'] as List).map((v) => OcrAttachmentEntity().fromJson(v)).toList();
	}
	return data;
}

Map<String, dynamic> paymentEntityToJson(PaymentEntity entity) {
	final Map<String, dynamic> data = new Map<String, dynamic>();
	data['data_type'] = entity.dataType;
	data['id'] = entity.id;
	data['flow_type'] = entity.flowType;
	data['type'] = entity.type;
	data['contact_id'] = entity.contactId;
	data['account_id'] = entity.accountId;
	data['payment_type_id'] = entity.paymentTypeId;
	data['issued_at'] = entity.issuedAt;
	data['numbers'] = entity.numbers;
	data['number_ids'] = entity.numberIds;
	data['currency_code'] = entity.currencyCode;
	data['total'] = entity.total;
	data['cheque_number'] = entity.chequeNumber;
	data['created_at'] = entity.createdAt;
	data['updated_at'] = entity.updatedAt;
	data['name'] = entity.name;
	data['account'] = entity.account?.toJson();
	data['to_account'] = entity.toAccount?.toJson();
	data['to_account_id'] = entity.toAccountId;
	data['related_payment_id'] = entity.relatedPaymentId;
	data['related_payment'] = entity.relatedPayment?.toJson();
	data['attachments'] =  entity.attachments?.map((v) => v.toJson())?.toList();
	return data;
}